package com.gitee.elf.command

import com.gitee.elf.core.backpack.BackpackManager
import com.gitee.elf.core.entity.AbstractBukkitProxyEntity
import com.gitee.elf.core.entity.EntityManager
import com.gitee.elf.core.entity.EntityManager.bukkitMappingInstance
import taboolib.common.platform.ProxyCommandSender
import taboolib.common.platform.command.*
import taboolib.common.platform.function.info
import taboolib.common5.cint

@CommandHeader("elf", aliases = ["pet"])
object CommandElf {


    @CommandBody
    val test = subCommand {
        dynamic("player") {
            suggestPlayers()
            dynamic("id") {
                suggest { EntityManager.entityThemes.keys.toMutableList() }
                execute<ProxyCommandSender> { sender, context, argument ->
                    val player = context.playerOrNull("player") ?: return@execute

                    val backpack = BackpackManager.getBackpack(player.cast())
                    val entityInstance =
                        EntityManager.createEntityInstance(player.cast(), context["id"]) ?: return@execute
                    info("创建实体实例")
                    backpack.addPosition(entityInstance)
                    entityInstance.spawn()
                }
            }
        }
    }

    @CommandBody
    val spawn = subCommand {
        dynamic("player") {
            suggestPlayers()
            execute<ProxyCommandSender> { sender, context, argument ->
                val player = context.playerOrNull("player") ?: return@execute
                BackpackManager.getBackpack(player.cast()).getPositionByIndex(0)?.instance?.spawn()
            }
        }
    }

    @CommandBody
    val ride = subCommand {
        dynamic("player") {
            suggestPlayers()
            dynamic("index") {

                execute<ProxyCommandSender> { sender, context, argument ->
                    val player = context.playerOrNull("player") ?: return@execute
                    val backpack = BackpackManager.getBackpack(player.cast())
                    backpack.getActivePositions().getOrNull(argument.cint)?.instance?.bukkitMappingInstance?.addPassenger(player.cast())
                }
            }

            execute<ProxyCommandSender> { sender, context, argument ->
                val player = context.playerOrNull("player") ?: return@execute
                val backpack = BackpackManager.getBackpack(player.cast())
                backpack.getActivePositions().getOrNull(0)?.instance?.bukkitMappingInstance?.addPassenger(player.cast())
            }
        }
    }

    @CommandBody
    val destroy = subCommand {
        dynamic("player") {
            suggestPlayers()
            execute<ProxyCommandSender> { sender, context, argument ->
                val player = context.playerOrNull("player") ?: return@execute
                BackpackManager.getBackpack(player.cast()).getPositionByIndex(0)?.instance?.destroy()
            }
        }
    }

    @CommandBody
    val reload = subCommand {
        execute<ProxyCommandSender> { sender, context, argument ->
            BackpackManager.destroys()
            EntityManager.loadEntityThemes()
            sender.sendMessage("reload successfully")
        }
    }

}